/*
* $Id: Wizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import apiary.ui.BackScreen;
import java.util.Hashtable;

/**
 * User interface that leads a user through steps
 * @author lexus
 */
public abstract class Wizard
{
    /** List of data form at all steps */
    protected Hashtable formData;
    /** List of form in the order they appear*/
    protected Hashtable formsNext;
    /** First form of wizard */
    protected BackScreen firstForm;
    /** Form where it want to move when pressing Cancel */
    protected BackScreen cancelForm;

    /**
     * Constructor
     * @param form
     * Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    protected Wizard(BackScreen form){
        cancelForm = form;
        formData = new Hashtable();
        formsNext = new Hashtable();
    }

    /**
     * Gets form where it want to move when pressing Cancel
     * @return form
     */
    public BackScreen getCancelForm(){
        return cancelForm;
    }
    /**
     * Gets first form of wizard
     * @return form
     */
    public BackScreen getFirstForm(){
        return firstForm;
    }
    /**
     * Gets next form of wizard
     * @param form identificator last form
     * @return next form
     */
    public BackScreen getNextForm(Integer form){
        if(formsNext.containsKey(form))
            return (BackScreen) formsNext.get(form);
        return null;
    }

    /**
     * Saves result of wizard
     */
    public abstract void save();
    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public abstract void next(BackScreen nextForm);
    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public abstract boolean isOk(Integer idForm, Object dataForm);

    /**
     * Sets data of form that was entered by user
     * @param form identificator of form
     * @param data data of form
     */
    public void setData(Integer form, Object data){
        formData.put(form, data);
    }
    /**
     * Gets data of form that was entered by user
     * @param form identificator of form
     * @return data of form
     */
    public Object getData(Integer form){
        if(formData.containsKey(form))
            return formData.get(form);
        return null;
    }

    /**
     * Sets form where it want to move when pressing Cancel
     * @param cnclForm form
     */
    public void setCancelForm(BackScreen cnclForm) {
        cancelForm = cnclForm;
    }
}
